from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import mixins
from rest_framework.generics import GenericAPIView
from .serializers import *
from .models import *


# Create your views here.

class Other:
    def get(self, request):
        ser = self.serializers_class(self.get_queryset(), many=True)
        return Response(ser.data)


class CateAPIView(APIView, Other):
    serializers_class = CateSer

    def get_queryset(self):
        queryset = Cate.objects.all()
        return queryset

    # def get(self,request):
    #     ser = self.serializers_class(self.get_queryset(),many=True)
    #     return Response(ser.data)


class BooksAPIView(APIView, Other):
    serializers_class = BooksSer

    def get_queryset(self):
        queryset = Books.objects.all()
        return queryset
    # def get(self,request):
    #     ser = self.serializers_class(self.get_queryset(),many=True)
    #     return Response(ser.data)


# 改造我们写法  使用GenericAPIView  + Mixin类来做
# Mixin混入类 不能独立使用 只能和GenericAPIView 配合使用
class CateGenericAPIView(GenericAPIView, mixins.ListModelMixin, mixins.CreateModelMixin):
    # 提供相应的序列化类
    serializer_class = CateSer
    # 提供对应的查询集
    queryset = Cate.objects.all()

    # get请求可以在视图集中自动查找到get处理方法
    # 查询全部 需要继承mixin中的ListModelMixin
    def get(self, request):
        return self.list(request)

    # 添加数据要继承迷信中的CreateModelMixin
    def post(self, request):
        return self.create(request)


class CateDetailView(GenericAPIView, mixins.RetrieveModelMixin, mixins.DestroyModelMixin, mixins.UpdateModelMixin):
    # 提供相应的序列化类
    serializer_class = CateSer
    # 提供对应的查询集
    queryset = Cate.objects.all()

    # get查询单条数据时要继承mixins中的RetrieveModelMixin 必须传pk
    def get(self, request, pk):
        return self.retrieve(request)

    # put修改操作需要继承mixins中的UpdateModelMixin
    def put(self, request, pk):
        return self.update(request)

    # delete修改操作要继承mixins中的DestroyModelMixin
    def delete(self, request, pk):
        return self.destroy(request)


# 导入视图集
from rest_framework.viewsets import ViewSet,ModelViewSet,GenericViewSet
class CateViewSet(GenericViewSet,mixins.UpdateModelMixin,mixins.DestroyModelMixin,mixins.RetrieveModelMixin,mixins.CreateModelMixin,mixins.ListModelMixin):
    # 提供相应的序列化类
    serializer_class = CateSer
    # 提供对应的查询集
    queryset = Cate.objects.all()

    # def get(self, request):
    #     return self.list(request)
    #
    # def post(self, request):
    #     return self.create(request)
    #
    #
    # def found(self, request, pk):
    #     return self.retrieve(request)
    #
    #
    # def put(self, request, pk):
    #     return self.update(request)
    #
    # def delete(self, request, pk):
    #     return self.destroy(request)



from rest_framework.decorators import action
class CateViewSet2(ModelViewSet):
    # 提供相应的序列化类
    serializer_class = CateSer
    # 提供对应的查询集
    queryset = Cate.objects.all()

    # 定义一个方法action 返回id是偶数的分类
    # @action(methods=['GET'],detail=False) # detail False 不允许路由后面有参数
    # def query(self,request):
    #     queryset = Cate.objects.filter(id__in=(2,3,4))
    #     ser = CateSer(queryset,many=True)
    #     return Response(ser.data)

    @action(methods=['GET'], detail=True)  # detail False 不允许路由后面有参数
    def find(self, request,pk):
        queryset = Cate.objects.get(id=pk)
        ser = CateSer(queryset)
        return Response(ser.data)